﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace LotterySystem
{
    public partial class Form1 : Form
    {
        List<Student> students = new List<Student>();
        Random r = new Random();
        List<Student> source;
        string mode;
        int level1 = 1, level2 = 0, level3 = 0;
        public Form1()
        {
            InitializeComponent();
            radioButton1.CheckedChanged += CheckedChanged;
            radioButton2.CheckedChanged += CheckedChanged;
            radioButton3.CheckedChanged += CheckedChanged;
        }

        private void GetSource()
        {
            switch (mode)
            {
                case "男同学":
                    source = students.Where(s => s.Sex == "男").ToList();
                    break;
                case "女同学":
                    source = students.Where(s => s.Sex == "女").ToList();
                    break;
                default:
                    source = students;
                    break;
            }
        }

        private void CheckedChanged(object sender, EventArgs e)
        {
            mode = ((RadioButton)sender).Text;
            GetSource();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (var line in Properties.Resources.list.Split('\n'))
            {
                var t = line.Split('\t');
                students.Add(new Student(t[0], t[1], t[2]));
            }
            source = students;
        }

        int i = 1;
        
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            switch ((int)numericUpDown1.Value)
            {
                case 2:
                    level2 = 1;
                    level3 = 0;
                    break;
                case 3:
                    level2 = 1;
                    level3 = 1;
                    break;
                default:
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (level1 + level2 + level3 > source.Count)
            {
                MessageBox.Show("中奖人数大于总人数，无法抽奖");
                return;
            }

            if (i == 1)
            {
                L_level1.Text = "";
                L_level2.Text = "";
                L_level3.Text = "";
            }

            if(i <= level1)
            {
                L_level1.Text = GetLotteryText( L_level1.Text);
            }
            else if(i > level1 && i <= level1 + level2)
            {
                L_level2.Text = GetLotteryText(L_level2.Text);
            }
            else
            {
                L_level3.Text = GetLotteryText(L_level3.Text);
            }

            i++;
            if(i > level1 + level2 + level3)
            {
                i = 1;
                GetSource();
                MessageBox.Show("抽奖完毕");
            }
        }

        
        private string GetLotteryText(string origin)
        {
            Student s = source[r.Next(0, source.Count - 1)];
            source.Remove(s);
            return string.Format("{0}{1}", origin, s);
        }

    }
}
